﻿/*
 *  RetroGlider v1.0
    Copyright (C) 2013 Alexander Blohmé

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenHack;
using System.IO;


namespace RetroGlider
{
    public partial class mainFrm : Form
    {

        public WoWEngine WoWInformation = new WoWEngine();
        delegate void SetTextCallback(string msg);
           
        public mainFrm()
        {
            InitializeComponent();

           
       
        }

        private void startBtn_Click(object sender, EventArgs e)
        {
            if (WoWInformation.InitWoW())
            {
                timer.Enabled = true;
            }

            WalkEngine walker = new WalkEngine();
            
            walker.Patrol();
            
            ItemSpellInteraction Spells = new ItemSpellInteraction();
            
            //Spells.CastSpell("Fireball", 1);
            //Spells.GetLocalizedText("hello");   

            // FOR LATER USE:
            // /script RepopMe();-- this command will release spirit
        }



        private void mainFrm_Load(object sender, EventArgs e)
        {
            OutPutMessage("welcome to RetroGlider v1.0\n");
        }

        private void aboutRetroGliderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            aboutFrm omProgram = new aboutFrm();
            omProgram.ShowDialog();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            statusText.Text = "Name: " + WoWInformation.PlayerName() + " | Level: " + WoWInformation.PlayerLevel() + " | Health: " + WoWInformation.PlayerHealth() + "/" + WoWInformation.PlayerMaxHealth() + " | Pos: Y: " + WoWInformation.PlayerYPosition() + " Pos X: " + WoWInformation.PlayerXPosition() + " | Facing: " + WoWInformation.PlayerFacingPosition();

            if (WoWInformation.HaveTarget())
            {
                targetLabel.Text = "Name: " + WoWInformation.TargetName() + " | Level: " + WoWInformation.TargetLevel() + " | Health: " + WoWInformation.TargetHealth() + "/" + WoWInformation.TargetMaxHealth() + " | Pos: Y: " + WoWInformation.TargetYPosition() + " Pos X: " + WoWInformation.TargetXPosition();
            }
            else
            {
                targetLabel.Text = "";
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (openWaypointDialog.ShowDialog() == DialogResult.OK)
            {
                WoWInformation.WalkSystem.LoadProfile(openWaypointDialog.FileName);
                OutPutMessage("*** Loaded Glider profile: " + openWaypointDialog.FileName);
            }
        }

        private void openWaypointDialog_FileOk(object sender, CancelEventArgs e)
        {
            
        }

        private void waypointCreatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            addWaypoints waypointWindow = new addWaypoints();
            waypointWindow.ShowDialog();
        }

        #region Output/Log
        public void OutPutMessage(string msg)
        {
            string space = " ";
            string bracket_left = "[";
            string bracket_right = "]";
            string time = DateTime.Now.ToShortTimeString();

            msg = bracket_left + time + bracket_right + space + msg;

            if (this.logger.InvokeRequired)
            {
                // It's on a different thread, so use Invoke.
                SetTextCallback d = new SetTextCallback(SetTextSystemLog);
                this.Invoke
                    (d, new object[] { msg });
            }
            else
            {
                // It's on the same thread, no need for Invoke
                logger.AppendText(msg + System.Environment.NewLine);
                logger.ScrollToCaret();
            }

            using (StreamWriter sw = File.AppendText("log.txt"))
            {
                sw.Write(msg + Environment.NewLine);
                sw.Close();
            }

        }

        private void SetTextSystemLog(string text)
        {
            this.logger.AppendText(text + System.Environment.NewLine);
            this.logger.ScrollToCaret();
        }

        public void UpdateText(System.Windows.Forms.Label lab, string text)
        {
            if (lab.InvokeRequired)
            {
                lab.BeginInvoke(
                    new MethodInvoker(
                    delegate() { UpdateText(lab, text); }));
            }
            else
            {
                lab.Text = text;
            }
        }
        #endregion
    }
}
